/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package tcc.sglj.action;

import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Result;
import org.apache.struts2.convention.annotation.ParentPackage;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.ServletActionContext;

import org.hibernate.Session;
import org.hibernate.Criteria;
import org.hibernate.criterion.Order;

import org.hibernate.criterion.Projections;
import org.hibernate.criterion.Restrictions;
import org.hibernate.criterion.ProjectionList;

import tcc.sglj.model.*;
import tcc.sglj.app.MaintainServiceApp;
import tcc.sglj.util.HibernateUtil;

import java.util.List;

/**
 *
 * @author Jhonatan L S Santos
 */
@Namespace(value = "/service")
@ParentPackage(value = "json-default")
public class MaintainServiceAction {

    private boolean success;
    private String message;
    private Service service;
    private int total;
    private int page;
    private int records;
    private int rows;
    private String sidx;
    private String sord;
    private List data;
    private int start;
    private String field;
    private String search;

    @Action(value = "sign", results =
    @Result(name = "success", type = "json", params = {"excludeProperties",
        "total, start, field, search, data, page, records, rows, sidx, sord"}))
    public String sign() {
        try {

            MaintainServiceApp maintain = new MaintainServiceApp();

            if (maintain.sign(getService())) {
                setSuccess(true);
                setMessage("Cadastro realizado com sucesso!");
            } else {
                setSuccess(false);
                setMessage(maintain.getErrorMessage());
            }

        } catch (Exception e) {
            setSuccess(false);
            setMessage(e.getMessage());
        }

        return "success";
    }

    @Action(value = "delete", results =
    @Result(name = "success", type = "json", params = {"excludeProperties",
        "total, start, field, search, data, page, records, rows, sidx, sord"}))
    public String delete() {
        try {

            MaintainServiceApp maintain = new MaintainServiceApp();
            boolean re = maintain.delete(Integer.parseInt(ServletActionContext.getRequest().getParameter("id")));
            
            if (re) {
                setSuccess(true);
                setMessage("Exclusão realizado com sucesso!");
            } else {
                setSuccess(false);
                setMessage(maintain.getErrorMessage());
            }

        } catch (Exception e) {
            setSuccess(false);
            setMessage(e.getMessage());
        }
        return "success";
    }

    @Action(value = "list", results =
    @Result(name = "success", type = "json", params = {"excludeProperties",
        "message, order, sidx, sord, rows, service, success"}))
    public String list() {

        try {
            Session session = HibernateUtil.getSession();

            this.setRecords(Integer.parseInt(session.createCriteria(Service.class).add(Restrictions.eq("status", true)).setProjection(Projections.rowCount()).list().iterator().next().toString()));

            this.setTotal((int) Math.ceil((double) getRecords() / (double) getRows()));

            setStart(getRows() * getPage() - getRows());

            Criteria crit = session.createCriteria(Service.class);
            ProjectionList projection = Projections.projectionList();

            projection.add(Projections.property("id"));
            projection.add(Projections.property("name"));
            projection.add(Projections.property("description"));
            projection.add(Projections.property("price"));
            crit.add(Restrictions.eq("status", true));

            crit.setProjection(projection);


            if (getSord().equals("desc")) {
                crit.addOrder(Order.desc(getSidx()));
            } else {
                crit.addOrder(Order.asc(getSidx()));
            }


            if (getField() != null && getSearch() != null) {
                if (getField().equals("id")) {
                    crit.add(Restrictions.eq(getField(), Integer.parseInt(getSearch())));
                } else if (getField().equals("price")) {
                    crit.add(Restrictions.eq(getField(), Double.parseDouble(getSearch())));
                } else if (getField().equals("name")) {
                    crit.add(Restrictions.ilike(getField(), "%" + getSearch() + "%"));
                }
            }

            crit.setFirstResult(getStart());
            crit.setMaxResults(getRows());

            this.setData(crit.list());

            if (getField() != null && getSearch() != null) {
                this.setRecords(this.getData().size());
            }

        } catch (Exception e) {
            e.getCause().toString();
        }
        return "success";
    }
    
   @Action(value = "teste", results = @Result(name = "success", location = "/teste.jsp"))

    public String teste() {
       service = new Service();
       service.setName("teste");
       service.setPrice(10.00);
       service.setStatus(true);
       return "success";
    }

    /**
     * @return the success
     */
    public boolean isSuccess() {
        return success;
    }

    /**
     * @param success the success to set
     */
    public void setSuccess(boolean success) {
        this.success = success;
    }

    /**
     * @return the message
     */
    public String getMessage() {
        return message;
    }

    /**
     * @param message the message to set
     */
    public void setMessage(String message) {
        this.message = message;
    }

    /**
     * @return the service
     */
    public Service getService() {
        return service;
    }

    /**
     * @param service the service to set
     */
    public void setService(Service service) {
        this.service = service;
    }

    /**
     * @return the total
     */
    public int getTotal() {
        return total;
    }

    /**
     * @param total the total to set
     */
    public void setTotal(int total) {
        this.total = total;
    }

    /**
     * @return the page
     */
    public int getPage() {
        return page;
    }

    /**
     * @param page the page to set
     */
    public void setPage(int page) {
        this.page = page;
    }

    /**
     * @return the records
     */
    public int getRecords() {
        return records;
    }

    /**
     * @param records the records to set
     */
    public void setRecords(int records) {
        this.records = records;
    }

    /**
     * @return the rows
     */
    public int getRows() {
        return rows;
    }

    /**
     * @param rows the rows to set
     */
    public void setRows(int rows) {
        this.rows = rows;
    }

    /**
     * @return the sidx
     */
    public String getSidx() {
        return sidx;
    }

    /**
     * @param sidx the sidx to set
     */
    public void setSidx(String sidx) {
        this.sidx = sidx;
    }

    /**
     * @return the sord
     */
    public String getSord() {
        return sord;
    }

    /**
     * @param sord the sord to set
     */
    public void setSord(String sord) {
        this.sord = sord;
    }

    /**
     * @return the data
     */
    public List getData() {
        return data;
    }

    /**
     * @param data the data to set
     */
    public void setData(List data) {
        this.data = data;
    }

    /**
     * @return the start
     */
    public int getStart() {
        return start;
    }

    /**
     * @param start the start to set
     */
    public void setStart(int start) {
        this.start = start;
    }

    /**
     * @return the field
     */
    public String getField() {
        return field;
    }

    /**
     * @param field the field to set
     */
    public void setField(String field) {
        this.field = field;
    }

    /**
     * @return the search
     */
    public String getSearch() {
        return search;
    }

    /**
     * @param search the search to set
     */
    public void setSearch(String search) {
        this.search = search;
    }
}